<script setup>
import { reactive, onMounted, watch } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
const data = reactive({
  // 路由返回携带的状态
  id: router.currentRoute.value.params.id,
  // 剩余秒数
  num: 5,
});

onMounted(() => {
  setTimeout(() => {
    data.num--;
  }, 1000);
});

watch(
  () => data.num,
  (newl, oldl) => {
    if (newl > 0) {
      setTimeout(() => {
        data.num--;
      }, 1000);
    } else {
      router.push("/");
    }
  }
);

// 返回上一页
const goBack = () => {
  router.go(-1);
};
// 返回主页
const goHome = () => {
  router.push("/son");
};
</script>

<template>
  <div class="box">
    <div class="img_box">
      <img src="../../assets/imgs/err.png" alt="" />
    </div>
    <div class="font20" v-if="data.id === '500'">
      <div class="mb20">糟糕发生错误了！！！{{ data.id }} 服务器发生错误</div>
      <div>还有{{ data.num }}秒返回主页</div>
      <el-button type="info" @click="goBack()">返回上一级</el-button>
      <el-button type="warning" @click="goHome()">返回主页</el-button>
    </div>
    <div class="font20" v-if="data.id === '404'">
      <div class="mb20">糟糕发生错误了！！！{{ data.id }} 客户端路径错误</div>
      <div>还有{{ data.num }}秒返回主页</div>
      <el-button type="info" @click="goBack()">返回上一级</el-button>
      <el-button type="warning" @click="goHome()">返回主页</el-button>
    </div>
  </div>
</template>

<style lang="less">
.mb20 {
  margin-bottom: 20px;
}
.font20 {
  font-size: 20px;
}
.box {
  width: 500px;
  height: 400px;
  padding-top: 100px;
  box-sizing: border-box;
  box-shadow: 0px 0px 10px 2px #dacbcb;
  .img_box {
    width: 160px;
    height: 160px;
    margin: 0 auto;
  }
}
</style>